package com.ruoyi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.entity.BrandModel;
import com.ruoyi.admin.domain.vo.response.brand.BrandPageResponse;
import com.ruoyi.admin.domain.vo.response.brand.BrandResponse;
import com.ruoyi.admin.mapper.BrandMapper;
import com.ruoyi.admin.service.BrandService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.api.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 品牌(Brand)Service实现类
 *
 * @author jianhan
 * @date 2023-01-31 10:49:28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandModel> implements BrandService {

    private static final Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);

    @Override
    public Result<BrandResponse> getId(Long brandId) {
        BrandModel brandModel = this.getById(brandId);
        if (brandModel != null) {
            BrandResponse brandResponse = new BrandResponse(brandModel);
            return Result.success(brandResponse);
        }
        return Result.failed(ResultCode.DATA_NOT_EXIST);
    }

    @Override
    public Result<BrandPageResponse> page(Page<BrandModel> page, BrandModel searchModel) {
        QueryWrapper<BrandModel> queryWrapper = queryWrapper(searchModel);

        Page<BrandModel> pages = this.page(page, queryWrapper);
        BrandPageResponse brandPageResponse = new BrandPageResponse(pages);
        page.getRecords().forEach((brandModel) -> brandPageResponse.getRecords().add(new BrandResponse(brandModel)));
        return Result.success(brandPageResponse);

    }

    @Override
    public Result add(BrandModel brandModel) {
        List<BrandModel> brand = this.list(new QueryWrapper<BrandModel>().eq("brand", brandModel.getBrand()));
        if (brand.size() > 0 && brand != null) {
            return Result.failed("已经存在该品牌");
        }
        Boolean result = this.save(brandModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("添加失败");
    }

    @Override
    public Result update(BrandModel brandModel) {
        Boolean result = this.updateById(brandModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("更新失败");
    }

    @Override
    public Result delete(Long brandId) {
        Boolean result = this.removeById(brandId);
        return Result.judge(result);

    }

    private QueryWrapper<BrandModel> queryWrapper(BrandModel brandModel) {
        QueryWrapper<BrandModel> queryWrapper = new QueryWrapper<>();
        
        /*
           写搜索条件
        */
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }

}
